import React from 'react';
import { connect } from 'dva';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import BatchUpload from '../../components/BatchUpload/Index';
import FileUpload from '../../components/BatchUpload/Upload';
import Finish from '../../components/BatchUpload/Finish';

@connect(({ batchreplied, publicdata, loading }) => ({
  uploading: loading.effects['batchreplied/uploadFileWithProgress'],
  fileId: publicdata.downloadFile.fileId,
  fileName: publicdata.downloadFile.fileName,
  currentStep: batchreplied.currentStep,
  progressPercent: batchreplied.progressPercent,
  resultInfo: batchreplied.resultInfo,
  showProgress: batchreplied.showProgress,
}))
class Batch extends React.PureComponent {
  componentDidMount() {
    const { dispatch } = this.props;
    // 下载模板
    dispatch({
      type: 'batchreplied/getDownloadInfo',
      payload: {
        batchAdjType: '3',
      },
    });
    this.clearModel();
  }

  componentWillUnmount() {
    this.clearModel();
  }

  // 重置model
  clearModel = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'batchreplied/updateModelData',
      payload: {
        showProgress: false,
        progressPercent: 0,
        currentStep: 0,
      },
    });
  };

  // 设置当前位置
  setCurrentstep = currentStep => {
    const { dispatch } = this.props;
    dispatch({
      type: 'batchreplied/updateModelData',
      payload: { currentStep },
    });
    if (!currentStep) {
      this.clearModel();
    }
  };

  // 上传文件：需要携带参数即传：batchQuery
  uploadFile = formData => {
    const { dispatch } = this.props;
    dispatch({
      type: 'batchreplied/uploadFileWithProgress',
      payload: formData,
    })
  };

  render() {
    const {
      fileId,
      fileName,
      uploading,
      currentStep,
      showProgress,
      resultInfo,
      progressPercent,
    } = this.props;
    return (
      <PageHeaderWrapper>
        <BatchUpload currentStep={currentStep}>
          <FileUpload
            title="上传文件"
            currentStep={currentStep}
            setCurrentstep={this.setCurrentstep}
            downloadTempInfo={{
              // 下载模板信息
              params: { fileId, fileName },
              url: '/csApi/batchTask/downloadFile',
            }}
            uploadFile={this.uploadFile}
            uploading={uploading}
            // 展示进度条需要的数据
            progressPercent={progressPercent || 0}
            progressTitle="批量回单任务"
            progressVisible={showProgress}
          />
          <Finish title="完成" resultInfo={resultInfo} setCurrentstep={this.setCurrentstep} />
        </BatchUpload>
      </PageHeaderWrapper>
    );
  }
}

export default Batch;
